/**
 * 
 */
package com.es.dao;


import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.apache.log4j.Logger;

import com.es.dao.factory.SqlMapClientFactory;
import com.es.exception.dao.ESDataAccessException;
import com.es.model.Lead;
import com.es.util.ProjectConstant;
import com.ibatis.sqlmap.client.SqlMapClient;


/**
 * @author Bushra Nazir
 *
 */
public class LeadDAO extends BaseDAO {

	private static final Logger log = Logger.getLogger(LeadDAO.class);




	/**
	 * @author Bushra
	 * save lead's model object in the repositary
	 * @param 'lead' model object
	 * @return lead id generated by db
	 * @throws ESDataAccessException
	 */

	public  int saveLead(Lead lead) throws ESDataAccessException {

		log.info(" inside saveLead() "); 
		log.info("SAVING LEAD..");

		SqlMapClient sqlMap =  SqlMapClientFactory.getSqlMapClient();
		int leadID = -1;

		try {
			leadID =(Integer) sqlMap.insert("lead.abatorgenerated_insert", lead);
			log.info("SAVED LEAD ID :" + leadID);
			return leadID;
		} catch (Exception e) {
			log.error(e);
			throw new ESDataAccessException(e);

		}
	}



	/**
	 * @author Bushra
	 * retrieving lead by leadID
	 * @param integer - leadID
	 * @return lead 
	 * @throws ESDataAccessException
	 */

	public Lead getLeadByLeadId(Integer LeadId) {

		log.info(" inside getLeadByLeadById() "); 
		Lead lead = null;

		SqlMapClient sqlMap =  SqlMapClientFactory.getSqlMapClient();

		try {
			lead = (Lead) sqlMap.queryForObject("lead.selectLeadById",LeadId);			
			log.info("USER PASSWORD::" + lead.getFirstname());

			return lead;
		} catch (Exception e) {
			log.error(e);
			throw new ESDataAccessException(e);

		}
	}


	/**
	 *@author Bushra Nazir
	 *@param null
	 * @return List<Lead>
	 */
	public List<Lead> getAllDeadLeads() {
		
		String leadStatus = ProjectConstant.DEAD_LEAD_STATUS;
		log.info(" inside getAllDeadLeads() "); 
		List<Lead> deadLeadList = new  ArrayList<Lead> ();
		SqlMapClient sqlMap =  SqlMapClientFactory.getSqlMapClient();
		
		try {
		 	deadLeadList = sqlMap.queryForList("lead.selectByStatus", leadStatus);
			log.info("DEAD LEAD LIST SIZE::" + deadLeadList.size());

			return deadLeadList;
		} catch (Exception e) {
			log.error(e);
			throw new ESDataAccessException(e);

		}
	}



	public void deleteDeadLeadById(Integer leadId) {
		
		String leadStatus = ProjectConstant.DEAD_LEAD_STATUS;
		
		log.info(" inside deleteLeadById() "); 
		
		Hashtable<String, Object> paramMap = new Hashtable<String, Object> ();
		
		paramMap.put("leadStatus", leadStatus);
		paramMap.put("leadId", leadId);
				
		SqlMapClient sqlMap =  SqlMapClientFactory.getSqlMapClient();
		
		try {
			
			log.info(" inside try catch block "); 
			
		 	 int deletedLeadId = sqlMap.delete("lead.deleteDeadLeadById", paramMap);

		 	 log.info("deleted lead id :: " + deletedLeadId );
		 	 
		} catch (Exception e) {
			log.error(e);
			throw new ESDataAccessException(e);

		}
		
		
		
	}



	public List<Lead> getAllLeads() {

		log.info(" inside getAllLeads() "); 
		List<Lead> leadList = new  ArrayList<Lead> ();
		SqlMapClient sqlMap =  SqlMapClientFactory.getSqlMapClient();
		
		try {
			leadList = sqlMap.queryForList("lead.selectAllLeads", null);
			log.info("LEAD LIST SIZE ::" + leadList.size());

			return leadList;
		} catch (Exception e) {
			log.error(e);
			throw new ESDataAccessException(e);

		}
		
	}



	public Lead modifyLead(Lead lead) {
	
		log.info(" inside modifyLead() "); 
		int leadId;

		SqlMapClient sqlMap =  SqlMapClientFactory.getSqlMapClient();

		try {
			leadId = sqlMap.update("lead.abatorgenerated_updateByPrimaryKey", lead);			
			log.info("USER ::" + lead.getFirstname());

			return lead;
			
		} catch (Exception e) {
			log.error(e);
			throw new ESDataAccessException(e);

		}
		
	}



	

}
